# https://www.ku6.com/index
import json
import os
import re
import shutil

import requests
from lxml import etree

from util.headers import get_headers_init_user_agent
from util.logger import get_looger
logger = get_looger("酷6网.log", __file__)

root = 'static/酷6网/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)


url = 'https://www.ku6.com/index'
response = requests.get(url)
# print(response.status_code)
content = response.content.decode('utf-8')
# print(content)
# items = re.findall(r'<img src="https://rbv01.ku6.com/wifi/(.*?)"></a>', content)
html_str = response.text
tree = etree.HTML(html_str)
items = tree.xpath('//div/div[@class="video-item"]')
# print(items)
for item in items:
    title = item.xpath('./div/h3/a/text()')[0]
    # print(title)
    img = item.xpath('.//a[@class="video-image-warp"]/img/@src')[0]
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': url
    })
    thumb_response = requests.get(img, headers=headers)
    with open(f"{root}/{title}.jpg", "wb") as f:
        f.write(thumb_response.content)




# async def fetch(session, url):
#     async with (session.get(url) as response):
#         print(f"开始爬取 {url}")
#         html_str = await response.text()
#         tree = etree.HTML(html_str)
#         items = tree.xpath('//div[1]/div[@class="video-item"]')
#         for item in items:
#             line = {
#                 'title': item.xpath('./div/h3)[0]'),
#                 'img': item.xpath('./div/h3)[0]')
#             }
#             logger.info(json.dumps(line, ensure_ascii=False))
#         print(f"爬取 {url} 结束")